#include "SpriteManager.h"
#include <sstream>
#include <algorithm>
#include <iostream>
#include "Level.h"
using namespace sf;
using namespace std;
SpriteManager::SpriteManager()
{
	
	if (!font.loadFromFile(Level::getPath(TYPEWRITTER)))
	{
		// erreur...
	}
		
	if (!textureTank1.loadFromFile(Level::getPath(TANK1)))
	{
		// erreur...
	}
	if (!textureTank2.loadFromFile(Level::getPath(TANK2)))
	{
		// erreur...
	}
	if (!textureTank3.loadFromFile(Level::getPath(TANK3)))
	{
		// erreur...
	}
	if (!textureTank4.loadFromFile(Level::getPath(TANK4)))
	{
		// erreur...
	}
	if (!textureTank5.loadFromFile(Level::getPath(TANK5)))
	{
		// erreur...
	}

	if (!textureEnemy1.loadFromFile(Level::getPath(ENEMY1)))
	{
		// erreur...
	}
	if (!textureEnemy2.loadFromFile(Level::getPath(ENEMY2)))
	{
		// erreur...
	}
	if (!textureEnemy3.loadFromFile(Level::getPath(ENEMY3)))
	{
		// erreur...
	}
	if (!textureEnemyBOSS.loadFromFile(Level::getPath(ENEMYBOSS)))
	{
		// erreur...
	}

	//-----------------
	if (!textureBackground1.loadFromFile(Level::getPath(BACKGROUND1)))
	{
		// erreur...
	}

	if (!textureBackground2.loadFromFile(Level::getPath(BACKGROUND2)))
	{
		// erreur...
	}
	if (!textureBackground3.loadFromFile(Level::getPath(BACKGROUND3)))
	{
		// erreur...
	}
	if (!textureBackground4.loadFromFile(Level::getPath(BACKGROUND4)))
	{
		// erreur...
	}
	if (!textureBackground5.loadFromFile(Level::getPath(BACKGROUND5)))
	{
		// erreur...
	}

	if (!textureBackgroundEndGame.loadFromFile(Level::getPath(BACKGROUNDENDGAME)))
	{
		// erreur...
	}
	

	//-------------------------
	if (!textureEnemyBullet1.loadFromFile(Level::getPath(ENEMYBULLET1)))
	{
		// erreur...
	}

	if (!textureRocket1.loadFromFile(Level::getPath(ROCKET1)))
	{
		// erreur...
	}


	

	textureTank1.setSmooth(true);
	textureTank2.setSmooth(true);
	textureTank3.setSmooth(true);
	textureTank4.setSmooth(true);
	textureTank5.setSmooth(true);
	textureEnemy1.setSmooth(true);
	textureEnemy2.setSmooth(true);
	textureEnemy3.setSmooth(true);
	textureEnemyBOSS.setSmooth(true);
	textureBackground1.setSmooth(true);	
	textureBackground2.setSmooth(true);
	textureBackground3.setSmooth(true);
	textureBackground4.setSmooth(true);
	textureBackground5.setSmooth(true);
	textureEnemyBullet1.setSmooth(true);
	textureRocket1.setSmooth(true);
	text.setFont(font);
	
}



void SpriteManager::setText(string texte, int size, Color Color,Vector2f position)
{
	
	text.setString(texte);
	text.setCharacterSize(size);
	text.setColor(Color);
	text.setPosition(position);
}


sf::Text SpriteManager::getText()
{
	return text;
}


void SpriteManager::setTextureOnCharacter(Character& c)
{
	sprite.setRotation(0);


	if (spriteEnumToString[c.getSprite()].find("TANK") != string::npos)
	{		
		switch (c.getSprite())
		{
		case TANK1:
		{
					  size = textureTank1.getSize();
					  sprite.setTexture(textureTank1);
					  break;
		}

		case TANK2:
		{
					  size = textureTank2.getSize();
					  sprite.setTexture(textureTank2);
					  break;
		}

		case TANK3:
		{
					  size = textureTank3.getSize();
					  sprite.setTexture(textureTank3);
					  break;
		}

		case TANK4:
		{
					  size = textureTank4.getSize();
					  sprite.setTexture(textureTank4);
					  break;
		}



		case TANK5:
		{


					  size = textureTank5.getSize();
					  sprite.setTexture(textureTank5);
					  break;
		}

		default:
		{
				  
				   break;
		}


		}


	
		sprite.setTextureRect(sf::IntRect(0, 0, size.x / 2, size.y));
		sprite.setScale(sf::Vector2f(float(0.007*c.getSize().x), float(0.007*c.getSize().y)));
		sprite.setOrigin(float(size.x / 4), float(size.y / 2));
		sprite.setColor(Color(255, 255 * (c.getCurrentHealth() / c.getHealth()), 255 * (c.getCurrentHealth() / c.getHealth())));
		sprite.setPosition(c.getPosition().x + c.getSize().x / 2, c.getPosition().y + c.getSize().y / 2);
		sprite.setRotation(c.getOrientation().x + 180);
		setSpriteShadow(5);
		




	}
		


	if (spriteEnumToString[c.getSprite()].find("ENEMY") != string::npos)
	{
		switch (c.getSprite())
		{



		case ENEMY1:
		{
					   sprite.setTexture(textureEnemy1);					  
					   break;
		}
		case ENEMY2:
		{
					   sprite.setTexture(textureEnemy2);
					   break;
		}
		case ENEMY3:
		{
					   sprite.setTexture(textureEnemy3);
					   break;
		}
		case ENEMYBOSS:
		{
					   sprite.setTexture(textureEnemyBOSS);
					   break;
		}
		
		default:
		{
				   sprite.setTexture(textureEnemy1);
				   break;
		}


		}
		size = textureEnemy1.getSize();
		sprite.setOrigin(float(size.x / 2), float(size.y / 2));
		sprite.setTextureRect(sf::IntRect(0, 0, size.x, size.y));

		sprite.setScale(sf::Vector2f(float(0.005*c.getSize().x), float(0.005*c.getSize().y)));
		sprite.setColor(Color(255, 255 * (c.getCurrentHealth() / c.getHealth()), 255 * (c.getCurrentHealth() / c.getHealth())));
		sprite.setPosition(c.getPosition().x + c.getSize().x / 2, c.getPosition().y + c.getSize().y / 2);

		setSpriteShadow(25);

	}
	
	

}


void SpriteManager::setTextureOnBullet(Bullet& bullet)
{
	sprite.setRotation(0);

	switch (bullet.getSprite())
	{
	case ROCKET1:
	{


				  size = textureRocket1.getSize();
				  sprite.setTexture(textureRocket1);
				  sprite.setColor(Color(255, 255, 255));

				  sprite.setTextureRect(sf::IntRect(0, 0, size.x, size.y));
				  sprite.setScale(sf::Vector2f(float(0.15*bullet.getSize().x), float(0.15*bullet.getSize().y)));
				  sprite.setOrigin(float(size.x ), float(size.y));				  
				  sprite.setPosition(bullet.getPosition().x + bullet.getSize().x / 2, bullet.getPosition().y + bullet.getSize().y / 2);

				  float angle = (atan(bullet.getBulletOrientation().x / -bullet.getBulletOrientation().y) / Pi * 180);

				  if (-bullet.getBulletOrientation().y > 0)
				  {
					  sprite.setRotation(angle - 90);
				  }
				  else
				  {
					  sprite.setRotation(angle+90);
				  }
				  
				 
				  break;
	}
	case ENEMYBULLET1:
	{
				   sprite.setColor(Color(255, 255, 255));
				   size = textureEnemyBullet1.getSize();
				   sprite.setOrigin(float(size.x / 2), float(size.y / 2));
				   sprite.setTextureRect(sf::IntRect(0, 0, size.x, size.y));
				   sprite.setTexture(textureEnemyBullet1);
				   sprite.setScale(sf::Vector2f(float(0.15*bullet.getSize().x), float(0.15*bullet.getSize().y)));				 
				   sprite.setPosition(bullet.getPosition().x + bullet.getSize().x / 2, bullet.getPosition().y + bullet.getSize().y / 2);
				   break;
	}
	default:
	{
			   break;
	}

	}
}
void SpriteManager::setBackground(int level)
{
	sprite.setRotation(0);

	switch (level)
	{

	case 1:
	{
			  size = textureBackground1.getSize();
			  sprite.setTexture(textureBackground1);
			  break;
	}

	case 2:
	{
			  size = textureBackground2.getSize();
			  sprite.setTexture(textureBackground2);
			  break;

	}

	case 3:
	{
			  size = textureBackground3.getSize();
			  sprite.setTexture(textureBackground3);
			  break;

	}
	case 4:
	{
			  size = textureBackground4.getSize();
			  sprite.setTexture(textureBackground4);
			  break;

	}
	case 5:
	{
			  size = textureBackground5.getSize();
			  sprite.setTexture(textureBackground5);
			  break;

	}


	default:
	{
			size = textureBackground1.getSize();
			sprite.setTexture(textureBackground1);
			break;
	}
	}

	sprite.setTextureRect(sf::IntRect(0, 0, size.x, size.y));
	sprite.setOrigin(0, 0);
	sprite.setScale(sf::Vector2f(float(0.0010*size.x), float(0.0013*size.y)));
	sprite.setPosition(Vector2f(0,0));
	sprite.setColor(Color(255, 255, 255));
	



}

void SpriteManager::setTurret(Character& c)
{


	sprite.setRotation(0);
	if (spriteEnumToString[c.getSprite()].find("TANK") != string::npos)
	{
		switch (c.getSprite())
		{
		case TANK1:
		{	
				size = textureTank1.getSize();
				sprite.setTexture(textureTank1);
				break;
		}
		case TANK2:
		{
				size = textureTank2.getSize();
				sprite.setTexture(textureTank2);
				break;
		}
		case TANK3:
		{
				size = textureTank3.getSize();
				sprite.setTexture(textureTank3);
				break;
		}
		case TANK4:
		{
				size = textureTank4.getSize();
				sprite.setTexture(textureTank4);
				break;
		}
		case TANK5:
		{
				size = textureTank5.getSize();
				sprite.setTexture(textureTank5);
				break;
		}
		}

			
			sprite.setTextureRect(sf::IntRect(size.x / 2, 0, size.x / 2, size.y));
			sprite.setOrigin(float(size.x / 4), float(size.y / 2));
			sprite.setScale(sf::Vector2f(float(0.007*c.getSize().x), float(0.007*c.getSize().y)));
			float angle = (atan(c.getCursorOrientation().x / -c.getCursorOrientation().y) / Pi * 180);

			if (-c.getCursorOrientation().y > 0)
			{
				sprite.setRotation(angle + 180);
			}
			else
			{
				sprite.setRotation(angle);
			}

			sprite.setPosition(c.getPosition().x + c.getSize().x / 2, c.getPosition().y + c.getSize().y / 2);
			setSpriteShadow(2);



		}



	
	
	
}

void SpriteManager::setSpriteShadow(float distance)
{
	shadowSprite = sprite;
	shadowSprite.setPosition(sprite.getPosition().x + distance, sprite.getPosition().y + distance);
	shadowSprite.setColor(Color(0, 0, 0, 125));
}


Sprite SpriteManager::getSprite()
{
	return sprite;
}

Sprite SpriteManager::getShadowSprite()
{
	return shadowSprite;
}


void SpriteManager::setVideoOnSprite(int image)
{
	string imagePath = "./media/flag/image" + to_string(image)+ ".jpg";
	if (!videoTexture.loadFromFile(imagePath))
	{
		// erreur...
	}

	size = videoTexture.getSize();
	sprite.setOrigin(0, 0);
	sprite.setTextureRect(sf::IntRect(0, 0, size.x, size.y));
	sprite.setPosition(Vector2f(0, 0));
	sprite.setColor(Color(255, 255, 255));
	videoTexture.setSmooth(true);
	sprite.setTexture(videoTexture);
	sprite.setScale(Vector2f(float(0.8),float( 0.9)));
}

void SpriteManager::setSprite(sf::Vector2f position, sf::Vector2f scale, SpriteEnum spriteEnum)
{
	switch (spriteEnum)
	{
	case TANK1:
		break;
	case TANK2:
		break;
	case TANK3:
		break;
	case TANK4:
		break;
	case TANK5:
		break;
	case ENEMY1:
		break;
	case ENEMY2:
		break;
	case ENEMY3:
		break;
	case ENEMYBOSS:
		break;
	case ROCKET1:
		break;
	case ENEMYBULLET1:
		break;
	case BACKGROUND1:
		break;
	case BACKGROUND2:
		break;
	case BACKGROUND3:
		break;
	case BACKGROUND4:
		break;
	case BACKGROUND5:
		break;
	case BACKGROUNDENDGAME:
	{
		sprite.setColor(Color(255, 255, 255));
		sprite.setTexture(textureBackgroundEndGame);
		size = textureBackgroundEndGame.getSize();
		sprite.setTextureRect(sf::IntRect(0, 0, size.x, size.y));
		sprite.setOrigin(0, 0);
		sprite.setScale(scale);
		sprite.setPosition(position);
		sprite.setRotation(0);
		break;
	}
	case TYPEWRITTER:
		break;
	default:
		break;
	}
}